﻿/*
    Auteur : Frank Wang ( 28141932@qq.com )
    accordion：选项卡的初始化
    appendAccordion：新建按钮的初始卡
*/
(function ($) {
    $.fn.columnfields = function (option) {
        var settings = $.extend({
            tableID: 1
        }, option);

        control = this;
        $.ajax({
            type: "post",
            url: "/Admin/CommService.asmx/GetTableColumnList",
            dataType: "json",
            contentType: "application/json;charset=utf-8",
            data: "{ tableID:" + settings.tableID + " }",
            success: function (json, textStatus) {
                if (json.d != null) {
                    $(this).html("");
                    var html = "";
                    $.each(json.d, function (i, n) {
                        $(control).append("<input type='checkbox' value='" + n.Name + "' name='chkFields' id='chkFields" +
                            n.Name + "' " + (n.IsEnable ? "Checked" : "") + " />");
                        $(control).append("<label for='chkFields" + n.Name + "'>" + n.Label + "</label>&nbsp;");
                    });

                    $(control).find("input[type='checkbox']").each(function (n) {
                        if ($(this).attr("checked") == true) {
                            $(".column-" + $(this).attr("value")).show();
                        } else {
                            $(".column-" + $(this).attr("value")).hide();
                        }
                    });

                    $(control).find("input[type='checkbox']").click(function () {
                        var chk = this;
                        $.ajax({
                            type: "post",
                            url: "/Admin/CommService.asmx/SetTableColumnSelect",
                            dataType: "json",
                            contentType: "application/json;charset=utf-8",
                            data: "{ tableID:" + settings.tableID + ", columnName:'" + $(this).val() + "' }",
                            success: function (json, textStatus) {
                                if (json.d > 0) {
                                    if ($(chk).attr("checked") == true) {
                                        $(".column-" + $(chk).attr("value")).show();
                                    } else {
                                        $(".column-" + $(chk).attr("value")).hide();
                                    }
                                }
                            },
                            error: function (XMLHttpRequest, textStatus, errorThrown) {
                                alert('设置启用时发生网络错误！');
                                this;
                            }
                        });
                    });
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert('设置启用时发生网络错误！2222');
                this;
            }
        });
    };
})(jQuery);